/*
============================================================================
Name        : BlockSetGridCol.cpp
Author      : 
Version     :
Copyright   : 
Description : CBlockSetGridCol implementation
============================================================================
*/

#include <eStock_L2hy.rsg>

#include "CntrlCommFun.h"

#include "Global.h"
#include "eStockappui.h"
#include "BlockGrid.h"
#include "BlockHYGrid.h"
#include "BlockSetGridCol.h"

const TInt KMenuIdRestore = 200;
const TInt KMenuIdSave = 201;

CBlockSetGridCol::CBlockSetGridCol(CBaseBlock* bsBack):
CBlock(bsBack)
{
}

CBlockSetGridCol::~CBlockSetGridCol()
{
	if(m_pstrName != NULL)
	{
		m_pstrName->Reset();
		delete m_pstrName;
	}

	m_psID.Close();
}

CBlockSetGridCol* CBlockSetGridCol::NewLC(CBaseBlock* bsBack,const  RArray<TInt>& psIdArr,const CDesCArray* pstrArr)
{
	CBlockSetGridCol* self = new (ELeave)CBlockSetGridCol(bsBack);
	CleanupStack::PushL(self);
	self->ConstructL(psIdArr,pstrArr);
	return self;
}

CBlockSetGridCol* CBlockSetGridCol::NewL(CBaseBlock* bsBack,const RArray<TInt>& psArr,const CDesCArray* pstrArr)
{
	CBlockSetGridCol* self=CBlockSetGridCol::NewLC(bsBack,psArr,pstrArr);
	CleanupStack::Pop(); // self;
	return self;
}

void CBlockSetGridCol::ConstructL(const RArray<TInt>& psIdArr,const CDesCArray* pstrArr)
{
	CBlock::ConstructL();

	CGlobal::GetDesFromID(m_strTitle, LOC_BUF_STR_SETCOLUMN);

	for(TInt i=0; i<psIdArr.Count(); i++)
	{
		m_psID.Append(psIdArr[i]);
	}
	m_pstrName = new (ELeave)CDesCArrayFlat(pstrArr->Length());
	for(TInt i=0; i<pstrArr->Count(); i++)
	{
		m_pstrName->AppendL((*pstrArr)[i]);
	}

	CGlobal::GetDesFromID(m_strOK,LOC_BUF_MENU_OPTION);

	CGlobal::GetDesFromID( m_strColNum,LOC_BUF_STR_COLNUM );
	CGlobal::GetDesFromID( m_strIndName,LOC_BUF_STR_INDNAME );
	CGlobal::GetDesFromID(m_strFmt,LOC_BUF_STR_COLNUMFMT);

	InitPopMenu(0);

	SetScrollBar(m_nCurrent, m_psID.Count());

	m_nCurrent = 0;
}

void CBlockSetGridCol::SizeChanged()
{
	CBlock::SizeChanged();
	SetScrollBar(m_nCurrent, m_psID.Count());
}

void CBlockSetGridCol::BeforeDraw(CFbsBitGc& g) const
{
	iTitlebar->SetTitleText(m_strTitle);
	CBlock::BeforeDraw(g);
}

void CBlockSetGridCol::OnDraw(CFbsBitGc& g)const
{
	//g.SetPenColor(m_pGlobal->g_rgbHighlight);
	TInt strwidth = m_pGlobal->g_font->TextWidthInPixels(m_strIndName);

	//g.DrawText(m_strColNum,TPoint(0, m_pGlobal->m_nSpaceUp+m_pGlobal->g_nFontHeight));
	//g.DrawText(m_strIndName, 
	//	TPoint(m_nScreenWidth-strwidth, m_pGlobal->m_nSpaceUp+m_pGlobal->g_nFontHeight));

	TInt nStart = m_nCurrent/m_nLinesPerPage*m_nLinesPerPage;
	g.UseFont(m_pGlobal->g_font);
	for (TInt i=0; i<m_nLinesPerPage; i++)
	{
		TInt nPos = nStart+i;
		if (nPos>=m_psID.Count())
			break;
		TInt nY = m_rect.iTl.iY + m_nLineHeight*i;

		if (m_nCurrent==nPos)
		{
			DrawImageRect(g,0, nY, m_rect.Width(), nY+m_nLineHeight,1);
		}

		g.SetPenColor(m_pGlobal->g_rgbText);

		TBuf<10> buf;
		buf.Format(m_strFmt,nStart+i+1);
		g.DrawText(buf, TPoint(0, nY+m_pGlobal->m_nSpaceUp+m_pGlobal->g_nFontHeight));

		g.DrawText((*m_pstrName)[nPos],TPoint(m_rect.Width()-strwidth, nY+m_pGlobal->m_nSpaceUp+m_pGlobal->g_nFontHeight));

	}
}

void CBlockSetGridCol::OnKeyDown(TInt keyCode)
{
	if(menuBar->IsPopup())
	{
		if(menuBar->OnKeyDown(keyCode))
		{
			DrawNow();
			return;
		}
	}
	else if ( keyCode==m_pGlobal->g_nKeyCancel)
	{
		if(eStockUI->GetCurView() && eStockUI->GetCurView()->iContainer)
			eStockUI->GetCurView()->iContainer->DelBlock();
		return;
	}
	else if(keyCode==m_pGlobal->g_nKeyOK || keyCode==EStdKeyNkpAsterisk ||keyCode=='*' )
	{
		menuBar->SetPopup(ETrue);
		DrawNow();
	}
	else if (keyCode==m_pGlobal->g_nKeyUp || keyCode==m_pGlobal->g_nKeyDown)
	{
		TInt nItem = m_psID.Count();
		if (nItem>1)
		{
			TInt nOldCurrentItem = m_nCurrent;
			if (keyCode==m_pGlobal->g_nKeyUp)
			{
				m_nCurrent--;
				if (m_nCurrent<0)
					m_nCurrent = nItem-1;
			}
			else
			{
				m_nCurrent++;
				if (m_nCurrent>=nItem)
					m_nCurrent = 0;
			}
			if (nOldCurrentItem!=m_nCurrent)
			{
				if (m_bShowScroll)
					m_sbVert->SetPos(m_nCurrent);
				DrawNow();
			}
		}
	}
}

void CBlockSetGridCol::InitPopMenu(const TInt& /*nType*/)
{
	menuBar->m_subMenu.ResetAndDestroy();

	CPopMenu* setmenu = CPopMenu::NewL(0,LOC_BUF_STR_COLSETAS);
	for(TInt i=0; i<m_pstrName->Count(); i++)
	{
		setmenu->Append(CPopMenu::NewL(m_psID[i],(*m_pstrName)[i]));
	}
	menuBar->Append(setmenu);
	menuBar->Append(CPopMenu::NewL(KMenuIdRestore,LOC_BUF_STR_REDEFAULT));
	menuBar->Append(CPopMenu::NewL(KMenuIdSave,LOC_BUF_STR_SAVE2));
}

TBool CBlockSetGridCol::OnMenuEvent(TInt menuId)
{
	if(menuId == KMenuIdRestore)
	{
		//CBlockGrid* block = (CBlockGrid*)m_pBlockBack;
		CBlockGrid* grid = dynamic_cast<CBlockGrid*>(m_pBlockBack);
		CBlockHYGrid* hyGrid = dynamic_cast<CBlockHYGrid*>(m_pBlockBack);
		if(grid)
			grid->InitDefaultColName(m_psID,m_pstrName);
		else if(hyGrid)
			hyGrid->InitDefaultColName(m_psID,m_pstrName);
	}
	else if(menuId == KMenuIdSave)
	{
		CBlockGrid* grid = dynamic_cast<CBlockGrid*>(m_pBlockBack);
		CBlockHYGrid* hyGrid = dynamic_cast<CBlockHYGrid*>(m_pBlockBack);
		if(grid)
			grid->UpdateIndexColumn(m_psID,m_pstrName);
		else if(hyGrid)
			hyGrid->UpdateIndexColumn(m_psID,m_pstrName);
		//CBlockGrid* block = (CBlockGrid*)m_pBlockBack;
		//block->UpdateIndexColumn(m_psID,m_pstrName);
	}
	else
	{
		TInt psid = m_psID[m_nCurrent];
		TBuf<12> psname = (*m_pstrName)[m_nCurrent];

		for(TInt i=0; i<m_psID.Count(); i++)
		{
			if(m_psID[i] == menuId)
			{
				m_psID[i] = psid;
				m_pstrName->Delete(i);
				m_pstrName->InsertL(i,psname);
				break;
			}
		}
		CPopMenu* submenu = (CPopMenu*)menuBar->m_subMenu[0];
		m_psID[m_nCurrent] = menuId;

		m_pstrName->Delete(m_nCurrent);
		TBuf<10> temp;
		submenu->GetMenuText(menuId,temp);
		m_pstrName->InsertL(m_nCurrent,temp);
		
		if(0 != CntrlCommFun::ShowConfirmationQueryL(LOC_BUF_STR_SETTING_SOFT_TIPS,
				m_pGlobal->g_rgbText, m_pGlobal->g_rgbBackGround,
				m_pGlobal->g_rgbHighlight))
		{
			CBlockGrid* grid = dynamic_cast<CBlockGrid*>(m_pBlockBack);
			CBlockHYGrid* hyGrid = dynamic_cast<CBlockHYGrid*>(m_pBlockBack);
			if(grid)
				grid->UpdateIndexColumn(m_psID,m_pstrName);
			else if(hyGrid)
				hyGrid->UpdateIndexColumn(m_psID,m_pstrName);
		}
	}
	return ETrue;
}

void CBlockSetGridCol::OnMouseUp(const TPoint& aPt)
{
	TInt nPos = aPt.iY/m_nLineHeight-1;
	if (nPos>=0 && nPos<m_nLinesPerPage)
	{
		nPos += m_nCurrent/m_nLinesPerPage*m_nLinesPerPage;

		TInt nSize = m_psID.Count();
		if (nPos<nSize)
		{
			if (m_nCurrent!=nPos)
			{
				m_nCurrent = nPos;
				if (m_bShowScroll)
					m_sbVert->SetPos(m_nCurrent);
				DrawNow();
			}
		}
	}
}
